﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PayloadTracker.APRS
{


    public delegate void APRSUpdateDelegate(IAPRSProvider sender, APRSBeacon beacon);
    
    /// <summary>
    /// Base interface that all APRS providers must derrive from.
    /// </summary>
    [Serializable]
    public abstract class IAPRSProvider : System.Xml.Serialization.IXmlSerializable
    {
        public event APRSUpdateDelegate APRSUpdate;

        
        
        /// <summary>
        /// Used for identifying the provider during configuration.
        /// </summary>
        [System.Xml.Serialization.XmlAttribute]
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }
        string m_Name = string.Empty;

        public IAPRSProvider()
        {
        }
        public IAPRSProvider(string name)
        {
            m_Name = name;
        }
        /// <summary>
        /// Any modem initialization should be done here.
        /// </summary>
        public virtual void Initialize()
        {
        }

        /// <summary>
        /// Override this and do any modem specific processing here. then fire the base.OnAprsUpdate() to fire the beacon.
        /// </summary>
        /// <param name="beacon">Latest APRS Beacon</param>
        protected virtual void OnAprsUpdate(APRSBeacon beacon)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.
            APRSUpdateDelegate handler = APRSUpdate;
            
            Logger.LOG.APRSBeacon(beacon, null);
            
            if(handler != null)
                handler(this,beacon);

        }
        
        /// <summary>
        /// Start firing the OnAPRSUpdate event after this. 
        /// </summary>
        public abstract void Start();

        /// <summary>
        /// Stop firing the OnAPRSUpdate event after this.
        /// </summary>
        public abstract void Stop();

        /// <summary>
        /// Function that gets called when APRS modem needs to be polled.
        /// Will always be called regardless of calls to Start() and Stop() After Initialize().
        /// </summary>
        public abstract void Tick();

        /// <summary>
        /// Any modem statistics that you would like displayed should override the ToString() method.
        /// eg. Number of satellites visible etc etc.
        /// </summary>
        /// <returns>Modem statistics.</returns>
        public  virtual string ModemStatistics()
        {
            return "N/A";
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public abstract void ReadXml(System.Xml.XmlReader reader);

        public abstract void WriteXml(System.Xml.XmlWriter writer);
    
        #endregion
    }
}
